package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.entity.OutputValueDO;
import com.kaizeli.website.pojo.vo.OutputValueVO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.OutputValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 产值管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/output")
@RequiredArgsConstructor
@Tag(name = "产值管理", description = "产值记录管理相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class OutputValueController {
    
    private final OutputValueService outputValueService;
    
    @Operation(summary = "新增产值记录", description = "创建新的产值记录")
    @PostMapping
    public Result<Long> save(@RequestBody OutputValueDO outputValueDO) {
        log.info("新增产值记录：{}", outputValueDO);
        Long result = outputValueService.save(outputValueDO);
        return Result.success(result);
    }
    
    @Operation(summary = "更新产值记录", description = "根据ID更新产值记录")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @Parameter(description = "产值记录ID", required = true, example = "1")
            @PathVariable Long id,
            @RequestBody OutputValueDO outputValueDO) {
        log.info("更新产值记录，ID：{}", id);
        outputValueDO.setId(id);
        Boolean result = outputValueService.update(outputValueDO);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID查询产值记录", description = "根据ID查询产值记录详情")
    @GetMapping("/{id}")
    public Result<OutputValueVO> getById(
            @Parameter(description = "产值记录ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID查询产值记录，ID：{}", id);
        OutputValueVO result = outputValueService.getById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID删除产值记录", description = "根据ID删除产值记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(
            @Parameter(description = "产值记录ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID删除产值记录，ID：{}", id);
        Boolean result = outputValueService.deleteById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据员工ID查询产值记录", description = "根据员工ID查询产值记录列表")
    @GetMapping("/employee/{employeeId}")
    public Result<List<OutputValueVO>> listByEmployee(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId) {
        log.info("根据员工ID查询产值记录，员工ID：{}", employeeId);
        List<OutputValueVO> result = outputValueService.listByEmployee(employeeId);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目ID查询产值记录", description = "根据项目ID查询产值记录列表")
    @GetMapping("/project/{projectId}")
    public Result<List<OutputValueVO>> listByProject(
            @Parameter(description = "项目ID", required = true, example = "1")
            @PathVariable Long projectId) {
        log.info("根据项目ID查询产值记录，项目ID：{}", projectId);
        List<OutputValueVO> result = outputValueService.listByProject(projectId);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目编号查询产值记录", description = "根据项目编号查询产值记录列表")
    @GetMapping("/project-code/{projectCode}")
    public Result<List<OutputValueVO>> listByProjectCode(
            @Parameter(description = "项目编号", required = true, example = "TEST001")
            @PathVariable String projectCode) {
        log.info("根据项目编号查询产值记录，项目编号：{}", projectCode);
        List<OutputValueVO> result = outputValueService.listByProjectCode(projectCode);
        return Result.success(result);
    }
    
    @Operation(summary = "根据员工ID和账期查询产值记录", description = "根据员工ID和账期查询产值记录列表")
    @GetMapping("/employee/{employeeId}/period/{accountPeriod}")
    public Result<List<OutputValueVO>> listByEmployeeAndPeriod(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId,
            @Parameter(description = "账期", required = true, example = "2025-01")
            @PathVariable String accountPeriod) {
        log.info("根据员工ID和账期查询产值记录，员工ID：{}，账期：{}", employeeId, accountPeriod);
        List<OutputValueVO> result = outputValueService.listByEmployeeAndPeriod(employeeId, accountPeriod);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目编号和账期查询产值记录", description = "根据项目编号和账期查询产值记录列表")
    @GetMapping("/project-code/{projectCode}/period/{accountPeriod}")
    public Result<List<OutputValueVO>> listByProjectAndPeriod(
            @Parameter(description = "项目编号", required = true, example = "TEST001")
            @PathVariable String projectCode,
            @Parameter(description = "账期", required = true, example = "2025-01")
            @PathVariable String accountPeriod) {
        log.info("根据项目编号和账期查询产值记录，项目编号：{}，账期：{}", projectCode, accountPeriod);
        List<OutputValueVO> result = outputValueService.listByProjectAndPeriod(projectCode, accountPeriod);
        return Result.success(result);
    }
    
    @Operation(summary = "统计员工产值", description = "统计指定账期范围内员工的产值")
    @GetMapping("/employee/{employeeId}/sum")
    public Result<BigDecimal> sumOutputByEmployee(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId,
            @Parameter(description = "开始账期", required = true, example = "2025-01")
            @RequestParam String startPeriod,
            @Parameter(description = "结束账期", required = true, example = "2025-12")
            @RequestParam String endPeriod) {
        log.info("统计员工产值，员工ID：{}，开始账期：{}，结束账期：{}", employeeId, startPeriod, endPeriod);
        BigDecimal result = outputValueService.sumOutputByEmployee(employeeId, startPeriod, endPeriod);
        return Result.success(result);
    }
    
    @Operation(summary = "统计项目产值", description = "统计指定账期范围内项目的产值")
    @GetMapping("/project/{projectId}/sum")
    public Result<BigDecimal> sumOutputByProject(
            @Parameter(description = "项目ID", required = true, example = "1")
            @PathVariable Long projectId,
            @Parameter(description = "开始账期", required = true, example = "2025-01")
            @RequestParam String startPeriod,
            @Parameter(description = "结束账期", required = true, example = "2025-12")
            @RequestParam String endPeriod) {
        log.info("统计项目产值，项目ID：{}，开始账期：{}，结束账期：{}", projectId, startPeriod, endPeriod);
        BigDecimal result = outputValueService.sumOutputByProject(projectId, startPeriod, endPeriod);
        return Result.success(result);
    }
    
    @Operation(summary = "获取产值统计", description = "获取产值统计数据")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getOutputStatistics(
            @Parameter(description = "开始账期", required = true, example = "2025-01")
            @RequestParam String startPeriod,
            @Parameter(description = "结束账期", required = true, example = "2025-12")
            @RequestParam String endPeriod) {
        log.info("获取产值统计，开始账期：{}，结束账期：{}", startPeriod, endPeriod);
        Map<String, Object> result = outputValueService.getOutputStatistics(startPeriod, endPeriod);
        return Result.success(result);
    }
    
    @Operation(summary = "获取产值分配", description = "获取产值分配数据")
    @GetMapping("/distribution")
    public Result<List<Map<String, Object>>> getOutputDistribution(
            @Parameter(description = "项目编号", required = true, example = "TEST001")
            @RequestParam String projectCode,
            @Parameter(description = "账期", required = true, example = "2025-01")
            @RequestParam String accountPeriod) {
        log.info("获取产值分配，项目编号：{}，账期：{}", projectCode, accountPeriod);
        List<Map<String, Object>> result = outputValueService.getOutputDistribution(projectCode, accountPeriod);
        return Result.success(result);
    }
}

